/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package edu.upf.grupE.DDBB;

import Learning.PruebaProperties;
import edu.upf.grupE.DDBB.Interfaces.XmlDbDataSource;
import java.io.IOException;
import org.xmldb.api.base.Collection;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import org.xmldb.api.DatabaseManager;
import org.xmldb.api.base.Database;
import org.xmldb.api.base.XMLDBException;

/**
 *
 * @author Madhava Carrillo
 */
public class DataSource implements XmlDbDataSource {

    private static Collection collection;
    static private DDBBManager DataSource = null;
    private static DataSource datasource = null;
    private static Properties config;
    private static Database database;

    private DataSource() throws IOException, ClassNotFoundException, InstantiationException, IllegalAccessException {
        config = new Properties();
        config.load(PruebaProperties.class.getClassLoader().getResourceAsStream("config/DBB.properties"));
        String driver = config.getProperty("XmlDbDataSource/driverClassName");
        Class cl = Class.forName(driver);
        database = (Database) cl.newInstance();
    }

    public static DataSource getInstance() throws IOException {
        if (datasource == null) {
            try {
                datasource = new DataSource();
            } catch (ClassNotFoundException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (InstantiationException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(DataSource.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return datasource;
    }

    /**
     *
     * @return collection el metodo devuelve una collection
     * @throws org.xmldb.api.base.XMLDBException lanza exceptions
     */
    @Override
    public Collection getCollection() throws XMLDBException {
        if (collection == null) {

            String URI = config.getProperty("XmlDbDataSource/url");

            DatabaseManager.registerDatabase(database);

            // Obtain the collection and launch the query, obtaining a set o results.
            collection = DatabaseManager.getCollection(URI);

        }
        return collection;

    }

    /**
     *
     * @param aUserName Nombre de usuario
     * @param aPassword Contraseña
     * @return collection
     * @throws org.xmldb.api.base.XMLDBException lanza exceptions
     */
    @Override
    public Collection getCollection(final String aUserName, final String aPassword) throws XMLDBException {
        Collection collection = null;

        String URI = config.getProperty("XmlDbDataSource/url");
        DatabaseManager.registerDatabase(database);

        // Obtain the collection and launch the query, obtaining a set o results.
        collection = DatabaseManager.getCollection(URI);
        return collection;
    }
}
